// DlgLogon.cpp : implementation file
//

#include "stdafx.h"
#include "Server.h"
#include "DlgLogon.h"


// CDlgLogon dialog

IMPLEMENT_DYNAMIC(CDlgLogon, CDialog)

CDlgLogon::CDlgLogon(CWnd* pParent /*=NULL*/)
	: CDialog(CDlgLogon::IDD, pParent)
{

}

CDlgLogon::~CDlgLogon()
{
}

void CDlgLogon::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
}


BEGIN_MESSAGE_MAP(CDlgLogon, CDialog)
	ON_BN_CLICKED(IDOK, &CDlgLogon::OnBnClickedOk)
	ON_BN_CLICKED(IDCANCEL, &CDlgLogon::OnBnClickedCancel)
	ON_WM_CLOSE()
END_MESSAGE_MAP()
BOOL CDlgLogon::OnInitDialog()
{
	CDialog::OnInitDialog();
	GetDlgItem(IDC_EDIT_Password)->SetFocus();
	return TRUE;
}

BOOL CDlgLogon::PreTranslateMessage(MSG* pMsg) 
{
	DWORD t1=0;
	DWORD t2=0;

	t1=pMsg->message;
	t2=(DWORD)pMsg->wParam;

	if( t1==WM_KEYDOWN && (t2==VK_RETURN))
	{
		if( GetDlgItem(IDC_EDIT_Password) == GetFocus() )
		{
			GetDlgItem(IDOK)->SetFocus();
			return true;
		}			
		else if( GetDlgItem(IDOK) == GetFocus() )
		{
			OnBnClickedOk() ;
			return true;
		}
	}
	return CDialog::PreTranslateMessage(pMsg);
}
// CDlgLogon message handlers

void CDlgLogon::OnBnClickedOk()
{
	CString str;
	GetDlgItemText(IDC_EDIT_Password,str);

	bool ret = true ;
	DD_LOGIN_INFO logon;
	memset(&logon,0x00,sizeof(DD_LOGIN_INFO));
	char szLogFile[MAX_PATH];
	sprintf_s(szLogFile,"%sDkqSvrLg.bin",g_strCurrentPath);
	FILE *fp = fopen(szLogFile,"rb");
	if(fp!=NULL)
	{
		fread(&logon,1,sizeof(DD_LOGIN_INFO),fp);
		fclose(fp);fp=NULL;
	}
	if(strlen(logon.szPassword) > 0)
	{
		if(str.GetLength() == 0)
		{
			ret = false ;
		}
		else
		{
			if(str.Compare(logon.szPassword) != 0)
			{
				ret = false ;
			}
		}
	}
	else
	{
		if(str.GetLength() > 0)
		{
			ret = false ;
		}
	}
	if( !ret )
	{
		return ;
	}
	CDialog::EndDialog(1);
}

void CDlgLogon::OnBnClickedCancel()
{
	CDialog::EndDialog(0);
}

void CDlgLogon::OnClose()
{
	CDialog::EndDialog(0);
}
